package com.mxin.install.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.ScrollView;

import com.mxin.install.R;
import com.mxin.util.ScreenUtils;


public class MyScrollView extends ScrollView {

    private float maxHeight;
    private float screenWeight;

    public MyScrollView(Context context){
        super(context);
        init(context);
    }

    public MyScrollView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
        init(context);

    }

    public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyScrollView, defStyleAttr, 0 );
        maxHeight = a.getDimensionPixelSize(R.styleable.MyScrollView_maxHeight, 0);
        screenWeight = a.getFloat(R.styleable.MyScrollView_screenWeight, 0f);
        a.recycle();
        init(context);
    }

    private void init(Context context) {
        if(screenWeight > 0){
            maxHeight = ScreenUtils.getScreenHeight()/screenWeight;
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if(maxHeight>0){
            heightMeasureSpec = MeasureSpec.makeMeasureSpec((int) maxHeight, MeasureSpec.AT_MOST);
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}
